<?php
$status = '';

define("MAXVISIBLELINES", 3);

function microtime_float()
{
   // Return the current time in microseconds
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

function printInfo($info)
{
    print_r($info);
    die ("");
}

function magic_addslashes($str)
{
    // Add Slashes ONLY if PHP.INNI doesn't have 'magic_quotes_gpc' turn On.
    if (!get_magic_quotes_gpc())
        return addslashes($str);
    else
        return $str;
}

function getusername()
{
    $name = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR']));
    ereg ('(.*)-' , $name, $user);
    return ($user[1]);
}

function is_super()
{
    if (isset($_SESSION['users'][getusername()]) && $_SESSION['users'][getusername()]->super_priv == 'enable')
        return 1;
    else
        return 0;
}

function is_admin()
{
    if (isset($_SESSION['users'][getusername()]) && $_SESSION['users'][getusername()]->admin_priv == 'enable')
        return 1;
    else
        return 0;
}

function is_qapriv()
{
    if (isset($_SESSION['users'][getusername()]) && $_SESSION['users'][getusername()]->qa_priv == 'enable')
        return 1;
    else
        return 0;
}

function stringCollapse($field, $nbOfChars, $jobCounter, $stringToConvert, $displayNotes='')
{
    // Description

    // Only displaying a certain number of characters of the description.
    // A mouse over the description will allow the user to click and to see the full description (a collapse-expand)
    // Only doing it if the description is long enough!
    // A tooltip is also available
    $stringOutput = '';

    // Displaying the text in tooltip like it is with no modification to it
    $tooltip = htmlspecialchars($stringToConvert);
    $desc = htmlspecialchars($stringToConvert);

    if( strlen($desc) > $nbOfChars || substr_count($desc, "\n") >= MAXVISIBLELINES || substr_count($desc, "<br>") >= MAXVISIBLELINES)
    {
        // Then the description is too long
        // Wrapping the text (with \n) and taking only the first line
        $descWrap = wordwrap($desc, $nbOfChars, "\n", 0); // 0 for not cuts of the word
        $descExplode = explode("\n", $descWrap);
        $smallDesc = $descExplode[0];

        // Now, to show the return line, lets replace them with <br>
        $replace = '<br>';
        $smallDesc = str_replace(array("\n"), $replace, $smallDesc);
    }
    $longDesc = substr($desc, $nbOfChars);
    $longDesc = trim($longDesc);

    // To show the return line, lets replace them with <br>
    $replace = '<br>';
    $desc = str_replace(array("\n"),$replace,$desc);

    if( $longDesc == '' && substr_count($desc, "<br>") < MAXVISIBLELINES && substr_count($desc, "\n") < MAXVISIBLELINES)
    {
        // Displaying the text completely
        echo "  <td class='tdjobs'>\n";
        echo "      <table style='font-size:110%;font-family: Arial ,sans-serif;' width='100%' height='100%' border=0>\n";
        echo "          <tr>\n";
        if ($displayNotes != '')
            echo "              <td class='tdjobsdescription' align='left' width='5%'>".$displayNotes."</td>\n";
        echo "              <td class='tdjobsdescription'>".$desc ."</td>\n";
        echo "         </tr>\n";
        echo "      </table>\n";
        echo "  </td>\n";
    }
    else
    {
        $longDesc = $desc;

        // Collapse-Expand
        $divIdLong = $field.$jobCounter;
        $divIdShort = $field.$jobCounter."s"; // Adding a s for the short division id
        $stringOutput =  "  <td class='tdjobs' id='description'>\n";
        $stringOutput .= "      <table style='font-size:100%;font-family: Arial ,sans-serif;' width='100%' border='0'>\n";
        $stringOutput .= "          <tr width=100%>\n";
        if ($displayNotes != '')
            $stringOutput .= "              <td class='tdjobsdescription' align='left' width='5%'>".$displayNotes."</td>\n";
        $stringOutput .= "              <td class='tdjobsdescription'>\n";
        $stringOutput .= "                  <div title='$tooltip' id='".$divIdShort."' class='expands' onclick='showHideMe(\"".$divIdLong."\")'>[+] ".$smallDesc."</div>\n";
        $stringOutput .= "                  <div id='".$divIdLong."' class='expands' style='display:none' onclick='showHideMe(\"".$divIdLong."\")'>[-] ".$longDesc."</div>\n";
        $stringOutput .= "              </td>\n";
        $stringOutput .= "          </tr>\n";
        $stringOutput .= "      </table>\n";
        $stringOutput .= "  </td>\n";
        /* Also equal
        echo "  <td class='tdjobs'><div title='$tooltip' id=$divIdShort class='expands' onclick='showHideMe($divIdLong)'>[+] ".$smallDesc."</div>
                                                  <div id=$divIdLong style='display:none' onclick='showHideMe($divIdLong)'>[-] ".$longDesc."</div> </td>\n";
        */

        /* Scrollbar
        // Does not work with Firefox
        echo '  <td class="tdjobs"> <div style="margin:0px; padding:6px; border:0px inset; width:200px; height:100%; overflow:auto">' . $desc ." </div></td>\n";
        */

        /* Tooltip
        // Lets show only some characters... the rest will be in the tooltip
        // Does not quite work on Firefox - does not fully show the tooltip!
        // Does not work correctly with Firefox when text is really long!!!
        $smallDesc = substr($desc, 0, $nbOfChars);
        echo "  <td class='tdjobs' title='".$desc."'> " . $smallDesc ."</td>\n";
        */
    }
    return ($stringOutput);
}

function addArray(&$array, $id, $var)
{
    $tempArray = array($var => $id );
    $array = array_merge ($array, $tempArray);
}

function getDuration($start, $end)
{
    if ($end!= "")
    {
        $time_spend = $end - $start;
        return sprintf("%d", $time_spend/(60*60)).":".strftime("%M:%S", $time_spend);
    }
    else
    {
        return 'N/A';
    }

}

function convertTime($time)
{
    if ($time == "" || $time == "None")
        return "";
    else if (ereg("(^[a-z0-9]{2}:)", $time))
        return $time;
    else
        return strftime("%H:%M:%S", $time);
}

function updateStatus($string)
{
    $status = $string;
    echo "<table class='tableforms' width=95% border=1>\n";
    echo "  <tr class='trforms' width=100%>\n";
    echo "      <td>\n";
    echo "          <b>Status: ".$status."</b>\n";
    echo "      </td>\n";
    echo "  </tr>\n";
    echo "</table>\n";
    echo "<br>\n";
}

?>